package com.org.khtn.command.select;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.org.khtn.data.Data;
import com.org.khtn.data.DataTables;
import com.org.khtn.ultilities.Ultilities;

public class Select extends Command {
	final String className = "Select:";

	public Map<String, Multimap<String, String>> select(String fields) {
		final String methodName = "select():";
		String[] fieldArrs = fields.split(",");

		if (fields.equals("*") || resultData.containsKey("error"))
			return resultData;
		else {
			Map<String, Multimap<String, String>> result = new HashMap<String, Multimap<String, String>>();
			System.out.println(className + methodName + "resultData:"
					+ resultData);

			for (Map.Entry<String, Multimap<String, String>> d : resultData
					.entrySet()) {
				System.out.println(className + methodName + " 0");

				Multimap<String, String> tam = ArrayListMultimap.create();
				tam.clear();
				int length = fieldArrs.length;
				for (int i = 0; i < length; i++) {
					System.out.println(className + methodName + "i:" + i);

					String[] temp = fieldArrs[i].split("\\.");

					int lengthField = temp.length;
					System.out.println(className + methodName + "fieldArrs[i]:"
							+ fieldArrs[i]);
					System.out.println(className + methodName + "lengthField:"
							+ lengthField);

					System.out.println(className + methodName + " 1");

					if (lengthField == 2) {
						System.out.println(className + methodName + " 1.1");

						if (!tableExist(temp[0])) {
							{

								return Ultilities
										.createError("table not exist");
							}
						}
						int pos = 0;
						for (; pos < currentTable.size(); pos++) {
							// System.out.println(className + methodName +
							// "temp[0]: " +
							// temp[0]);
							System.out.println(className + methodName
									+ "currentTable.get(pos): "
									+ currentTable.get(pos));
							// System.out.println(className + methodName +
							// "pos 1: " + pos);

							if (currentTable.get(pos).equalsIgnoreCase(temp[0])) {

								// System.out.println(className + methodName +
								// "pos 2: " +
								// pos);

								break;
							}
						}

						if (temp[1].equals("*")) {
							System.out.println(className + methodName
									+ " 1.1.1");
							DataTables dt = new DataTables(new Data(
									databaseName, "sdas", "mytest\\data\\"));
							String ttt = dt.getTableName(temp[0]);
							Map<String, String> aa = tableDefine.get(ttt);
							for (Map.Entry<String, String> da : aa.entrySet()) {
								String keya = da.getKey();
								List<String> a = (List<String>) d.getValue()
										.get(keya);
								// System.out.println(className + methodName
								// + " keya: " + keya);
								// System.out.println(className + methodName
								// + " d.getValue(): " + d.getValue());
								//
								// System.out.println(className + methodName
								// + " a " + a);
								// System.out.println(className + methodName
								// + "pos: " + pos);
								//
								// System.out.println(className + methodName
								// + " a.get(pos) " + a.get(pos));
								if (a.size() > 1)
									tam.put(keya, a.get(pos));
								else
									tam.putAll(keya, a);
								System.out.println(className + methodName
										+ " tam: " + tam);

							}
						} else {
							if (ambiguousField(temp[1]))
								return Ultilities
										.createError("field not exist");
							DataTables dt = new DataTables(new Data(
									databaseName, "sdas", "mytest\\data\\"));
							String fieldName123 = dt.getFieldName(temp[0], temp[1]);
							List<String> aba = (List<String>) d.getValue().get(fieldName123);
							
							tam.put(fieldName123, aba.get(pos));
						}

					} else if (lengthField == 1) {
						System.out.println(className + methodName + " 1.2");

						if (ambiguousField(fieldArrs[i])) {
							return Ultilities.createError("ambiguous field");

						}
						System.out.println(className + methodName + " 1.2.2");
						System.out.println(className + methodName
								+ " fieldArrs[i]:" + fieldArrs[i]);

						tam.putAll(fieldArrs[i], d.getValue().get(fieldArrs[i]));

					}
				}
				result.put(d.getKey(), tam);
				System.out.println(className + "result" + result);

			}
			return result;
		}
	}

	// public Map<String, Multimap<String, String>> select(String string) {
	// // TODO Auto-generated method stub
	// return getRecord(string, data);
	// }

}
